package mylib.dir;

/**
 * Created with IntelliJ IDEA.
 * User: 1
 * Date: 10.08.12
 * Time: 14:56
 * To change this template use File | Settings | File Templates.
 */
public class Line implements Comparable<Line> {
    long A, B, C;

    public Line(Point A, Point B) {
        this.A = B.y - A.y;
        this.B = A.x - B.x;
        this.C = -this.A * A.x - this.B * A.y;
        norm();
    }

    private void norm() {
        long g = MathUtils.gcd(A, MathUtils.gcd(B, C));
        if (g != 0) {
            A /= g;
            B /= g;
            C /= g;
        }
        if (A < 0 || A == 0 && B < 0 || A == 0 && B == 0 && C < 0) {
            A = -A;
            B = -B;
            C = -C;
        }
    }

    long sign(Point point) {
        return A * point.x + B * point.y + C;
    }

    double distance(Point point) {
        return Math.abs(A * point.x + B * point.y + C) / Math.sqrt((double) A * A + (double) B * B);
    }

    @Override
    public int compareTo(Line o) {
        if (A != o.A)
            return Long.signum(A - o.A);
        if (B != o.B)
            return Long.signum(B - o.B);
        return Long.signum(C - o.C);
    }
}
